<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <!--<a-col :span="8">
            <a-form-item label="类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['type', validatorRules.type]" :trigger-change="true" dictCode="user_type" placeholder="请选择类"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="用户名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['username', validatorRules.username]" placeholder="请输入用户名"></a-input>
            </a-form-item>
          </a-col>-->
          <a-col :span="12">
            <a-form-item label="企业名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['name', validatorRules.name]" placeholder="请输入企业名称"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="法人姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['legalName']" placeholder="请输入法人姓名"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="手机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phone', validatorRules.phone]" placeholder="请输入手机号/法人手机号"></a-input>
            </a-form-item>
          </a-col>
          <!--<a-col :span="8">
            <a-form-item label="性别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['gender']" :trigger-change="true" dictCode="sex" placeholder="请选择性别"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['idNo']" placeholder="请输入身份证号/法人身份证"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="统一社会信用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['unionCredit']" placeholder="请输入统一社会信用"></a-input>
            </a-form-item>
          </a-col>

          <a-col :span="8">
            <a-form-item label="邮箱" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['email']" placeholder="请输入邮箱"></a-input>
            </a-form-item>
          </a-col>-->

          <!--<a-col :span="8">
            <a-form-item label="保证金" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['earnestMoney']" placeholder="" style="width: 100%" :min="0" />
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="冻结保证金" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['frozenEarnest']" placeholder="" style="width: 100%" :min="0" />
            </a-form-item>
          </a-col>-->
          <a-col :span="12">
            <a-form-item label="保证金充值" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['earnestMoney']" placeholder="" style="width: 100%" :min="0" />
            </a-form-item>
          </a-col>
          <!--<a-col :span="8">
            <a-form-item label="是否有效" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isAvailable']" :trigger-change="true" dictCode="valid_status" placeholder="请选择是否有效"/>
            </a-form-item>
          </a-col>
          <a-col :span="8">
            <a-form-item label="是否会员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['isMember']" :trigger-change="true" dictCode="valid_status" placeholder="请选择是否会员"/>
            </a-form-item>
          </a-col>-->
          <!--<a-col :span="24">
            <a-form-item label="申请状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['auditState']" :trigger-change="true" dictCode="audit_state" placeholder="请选择申请状态"/>
            </a-form-item>
          </a-col>-->

        </a-row>
      </a-form>
    </j-form-container>
<!--    <j-form-container :disabled="!formDisabled">-->
    <a-form :form="form">
      <a-row>
        <!--<a-col  :span="12">
          <a-form-item label="保证金充值" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input-number  placeholder="输入金额"  :formatter="value => `${value}元`"  style="width: 100%" :min="0" v-decorator="['amount',{initialValue:'2000' }, validatorRules.amount]" disabled/>
          </a-form-item>
        </a-col>-->
        <a-col  :span="12">
          <a-form-item label="银行卡号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <a-input  placeholder="输入银行卡号" style="width: 100%" v-decorator="['bankNo', validatorRules.bankNo]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col  :span="12">
          <a-form-item label="上传凭证" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <j-image-upload isMultiple v-decorator="['vouchers', validatorRules.vouchers]"></j-image-upload>
          </a-form-item>
        </a-col>

      </a-row>
      <a-row>
        <a-col  :span="24" style="text-align: center">
          <a-button @click="submitForm" type="primary">提 交</a-button>
        </a-col>
      </a-row>
    </a-form>
<!--    </j-form-container>-->
  </a-spin>
</template>

<script>

  import { httpAction, getAction,postAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JImageUpload from '@/components/jeecg/JImageUpload'

  export default {
    name: 'CustomerChargeForm',
    components: {
      JFormContainer,
      JDictSelectTag,
      JImageUpload
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        amount:0,
        labelCol: {
          /*xs: { span: 24 },
          sm: { span: 5 },*/
          span:8
        },
        wrapperCol: {
          /*xs: { span: 24 },
          sm: { span: 16 },*/
          span:16
        },
        confirmLoading: false,
        validatorRules: {
          bankNo: {
            rules: [
              { required: true, message: '请输入银行卡号!'},
            ]
          },
          amount: {
            rules: [
              { required: true, message: '请输入充值金额!'},
            ]
          },
          vouchers: {
            rules: [
              { required: true, message: '请上传充值凭证!'},
            ]
          },
          username: {
            rules: [
              { required: true, message: '请输入用户名!'},
            ]
          },
          name: {
            rules: [
              { required: true, message: '请输入姓名/名称!'},
            ]
          },
          phone: {
            rules: [
              { required: true, message: '请输入手机号/法人手机号!'},
            ]
          },
        },
        url: {
          add: "/customer/add",
          edit: "/customer/edit",
          queryById: "/customer/queryById",
          recharge:"/earnestMoneyRecord/recharge" //充值
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'name','phone','legalName','bankNo','amount','vouchers','earnestMoney'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;

            let formData = {
              amount:values.amount,
              bankNo:values.bankNo,
              customerId: this.model.customerId,
              propertyId: this.model.propertyId,
              amount:this.model.earnestMoney
            };
            let vouchers1 = []
            let vouchersArr = values.vouchers.split(',')
            // console.log('formData',this.model)
            console.log('vouchersArr',vouchersArr)
            for (let i = 0; i <vouchersArr.length ; i++) {
              let obj = {
                storeUrl:vouchersArr[i],
                type:'5'
              }
              vouchers1.push(obj)
            }

            let formData1 = Object.assign(formData,{vouchers:vouchers1})
            console.log("表单提交数据",formData1)
            postAction(this.url.recharge,formData1).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'type','username','name','phone','gender','frozenEarnest','idNo','unionCredit','legalName','email','earnestMoney','isAvailable','isMember','vouchers','amount','bankNo'))
      },
    }
  }
</script>